(function(window){
	var ajax = function(parameters){
		var success = parameters.success || function(data){
			var msg = arguments[1];
			if(msg != ''){
				Z.tip(msg, 2);
			}
		},
		error = parameters.error || function(resp){
			Z.alert(resp.msg||'操作失败');
		};

		parameters.type = parameters.type || "GET";
		parameters.dataType = parameters.dataType || "json";
		parameters.cache = parameters.cache || false;

		parameters.success = function(resp,status,xhr){
			if(resp == null){
				Z.log('\u670d务器端错误');
				return;
			}
			if(resp.status === undefined){
				success(resp);
				return;
			}

			if(resp.status == 1) {
				success(resp.data, resp.msg);
			} else {
				error(resp);
			}
		};

		parameters.error = function(jqXHR,status,errorThrown){
			Z.log(jqXHR,status,errorThrown);
		};

		$.ajax(parameters);
	};
	
	var route = function(controller, action, parameters){
		var url = ['php/index.php?controller=', controller, '&action=', action];
		for(var k in (parameters || {})) {
			url.push('&', k, '=', parameters[k])
		}
		return Z.url(url.join(''));
	};

	var tab = function(tabBtns,tabPans,classes,onTab){
		var show = function(index){
			$(tabBtns).removeClass(classes[0]).eq(index).addClass(classes[0]).show();
			$(tabPans).removeClass(classes[1]).hide().eq(index).addClass(classes[1]).show();
		};

		classes  = classes || ['active','active'];
		onTab = onTab || function(){};
		show(0);

		$(tabBtns).each(function(index){
			$(this).click(function(){
				show(index);
				onTab(index);
				return false;
			});
		});
	};

	var CirculateShow = function(target,width,height){
		var self = this;
		this.index = 0;
		this.buttons = $(target).find('ul.z-button li a');
		this.imageContainer = $(target).find('.z-image-container');
		this.image = $(target).find('.z-image-container img.z-image');
		this.interval = null;

		$(this.buttons).each(function(i){
			$(this).click(function(){
				window.clearInterval(self.interval);
				self.index = i;
				self.go(function(){
					self.tick();
				});
				return false;
			});
		});

		this.go(function(){
			self.tick();
		});
	};

	CirculateShow.prototype = {
		go : function(f){
			var src = $(this.buttons).eq(this.index).attr('href'),
			loading = Z.loading($(this.imageContainer)),
			self = this;
			$(this.image).hide().attr('src',src).load(function(){
				self.imageContainer.width($(this).width());
				self.imageContainer.height($(this).height());
				loading.hide();
				$(this).fadeIn(500,function(){
					(f||function(){})();
				});
			});
		},
		tick: function(){
			var length = this.buttons.length,self=this;
			if(length > 1){
				window.clearInterval(self.interval);
				this.interval = window.setInterval(function(){
					self.index++;
					if(self.index == length){
						self.index=0;
					}
					self.go();

					window.clearInterval(self.interval);
				},8000);
			}
		}
	};
	// end circulate show

	var parseFloat = function(num,fixed){
		return parseFloat(num==""?0:num).toFixed(fixed||2);
	};

	var parseInt = function(num){
		return parseInt(num) == NAN ? 0 : parseInt(num);
	};

	var innerTip = function(element,string){
		$(element).val(string).focus(function(){
			if($(this).val() == string){
				$(this).addClass('active').val('');
			}
		}).blur(function(){
			if($(this).val() == ''){
				$(this).removeClass('active').val(string);
			}else if($(this).val() == string){
				$(this).removeClass('active');
			}
		});
	};
	
	var repeat = function(c, t){
		var r = [];
		for(var i=0;i<t;i++){
			r.push(c);
		}
		return r.join('');
	};

	var validator = function(mode, string){
		if(validator.hasOwnProperty(mode))
			return validator[mode](string);
		return false;
	};
	validator.email = function(string){
		var normal = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/,
		qq = /^\d+@qq\.com$/;
		return normal.test(string) || qq.test(qq);
	};
	validator.url = function(string){
		return /^(ht|f)tps?\:\/\/.*?$/.test(string);
	};

	var format = function(object,_depth){
		var string = '',
		depth = _depth || 0,
		length = 0,
		children = '';
		
		if(++depth > 8){
			console.error('to deep');
			return false;
		}
		
		if(object == null){
			return 'null';
		}
		
		switch(object.constructor){
			case Number:
				string = '<span class="number">' + object.toString() + '</span>';
				break;
			case String:
				string = '<span class="string">' + object.toString() + '</span>';
				break;
			case Boolean:
				string = '<span class="boolean">' + object.toString() + '</span>';
				break;
			case Array:
				length = object.length;
				if(length > 0){
					string = '<span>('+length+') [</span><dl class="object">';
					for(var i=0;i<length;i++){
						string += '<dd class="key"><a href="javascript:void(0);">'+i+'</a> : ';
						children = arguments.callee(object[i],depth);
						if(!children){
							return string;
						}
						string += children + "</dd>";
					}
					string += '</dl><span>]</span>';
				}else{
					string = '<span>[]</span>';
				}
				break;
			case Object:
				var temp = '<dl class="object">';
				for(var p in object){
					if(object.hasOwnProperty(p)){
						length++;
						temp += '<dd class="key"><a href="javascript:void(0);">' + p + '</a> : ';
						children = arguments.callee(object[p],depth);
						if(!children){
							return string;
						}
						temp += children + '</dd>';
					}
				}
				temp += '</dl>';
				if(length)
					string = '<span>('+length+'){</span>'+temp+'<span>}</span>';
				else
					string = '{}';
				break;
			case Function:
			default:
				string = "can't display function."
				break;
		}
		
		return string;
	};
	
	$('#right').on('click', 'dl.object dd.key a', function() {
		var dl = $(this).siblings('dl');
		if (dl.length > 0) {
			$(this).toggleClass('closed');
			dl.toggle();
		}
	});

	window.Z.ajax = ajax;
	window.Z.route = route;
	window.Z.CirculateShow = CirculateShow;
	window.Z.validator = validator;
	window.Z.innerTip = innerTip;
	window.Z.format = format;
	window.Z.tab = tab;
	window.Z.repeat = repeat;
})(window);